<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>教室管理</title>
    <style>
        /* 整体页面布局 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }
        /* 导航栏样式 */
        nav {
            background-color: #333;
            width: 100%;
            max-width: 1800px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 20px;
        }
        nav a {
            color: white;
            text-decoration: none;
            padding: 10px 15px;
            /* 设置导航栏链接之间的距离为 20px */
            margin-right: 20px;
        }
        nav a:hover {
            background-color: #ddd;
            color: black;
        }
        /* 表格容器样式 */
        .table-container {
            background-color: #fff;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 95%;
            max-width: 1800px;
            margin-top: 20px;
        }
        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            font-size: 1.1em;
        }
        /* 斑马条纹表格行 */
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        /* 操作列样式 */
        td button {
            display: inline-block;
            padding: 6px 12px;
            background-color: #007BFF;
            color: white;
            text-decoration: none;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        td button:hover {
            background-color: #0056b3;
        }
        /* 筛选表单样式 */
        .filter-form {
            background-color: #fff;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            width: 95%;
            max-width: 1800px;
            margin-top: 30px;
        }
        .filter-form label {
            margin-right: 10px;
        }
        .filter-form input[type="text"],.filter-form input[type="date"] {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .filter-form input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .filter-form input[type="submit"]:hover {
            background-color: #45a049;
        }
        /* 新增教室按钮样式 */
        .add-classroom-button {
            display: inline-block;
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            text-decoration: none;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            margin-bottom: 20px;
        }
        .add-classroom-button:hover {
            background-color: #0056b3;
        }
    </style>
    <!-- 或者使用最新的 jQuery 版本 -->
    <script src="${pageContext.request.contextPath}/js/jquery-3.7.1.min.js"></script>
    <script>
        function showAddClassroomDialog() {
            // 显示新增教室的对话框，可以使用模态框等，这里简单使用 prompt 作为示例
            const classroomName = prompt("请输入教室名称:");
            const building = prompt("请输入教学楼:");
            const description = prompt("请输入教室描述:");
            const capacity = prompt("请输入教室容量:");
            if (classroomName && building && description && capacity) {
                $.ajax({
                    type: "POST",
                    url: "/addClassroom",
                    data: {
                        classroomName: classroomName,
                        building: building,
                        description: description,
                        capacity: capacity
                    },
                    success: function() {
                        // 刷新页面或更新教室信息列表
                        location.reload();
                    },
                    error: function() {
                        alert("添加教室请求失败，请稍后重试。");
                    }
                });
            }
        }
        $(document).ready(function() {
            // 编辑教室
            $('.edit-button').click(function() {
                const classroomId = $(this).data('id');
                // 根据classroomId获取教室信息，并填充到编辑模态框中
                // 这里假设你有一个函数getClassroomById(classroomId)来获取教室信息
                const classroom = getClassroomById(classroomId);
                $('#editId').val(classroomId);
                $('#editClassroomName').val(classroom.classroomName);
                $('#editBuilding').val(classroom.building);
                $('#editCapacity').val(classroom.capacity);
                $('#editStatus').val(classroom.status);
                $('#editClassroomModal').modal('show');
            });

            // 删除教室
            $('.delete-button').click(function() {
                const classroomId = $(this).data('id');
                if (confirm('确定要删除该教室吗？')) {
                    $.ajax({
                        type: "POST",
                        url: "/deleteClassroom",
                        data: {
                            id: classroomId
                        },
                        success: function() {
                            // 刷新页面或更新教室信息列表
                            location.reload();
                        },
                        error: function() {
                            alert("删除教室请求失败，请稍后重试。");
                        }
                    });
                }
            });
        });

        function getClassroomById(classroomId) {
            // 根据classroomId获取教室信息的函数，这里只是一个示例
            // 实际上你需要根据你的后端实现来获取教室信息
            return {
                id: classroomId,
                classroomName: '教室名称',
                building: '教学楼',
                capacity: 100,
                status: '可用'
            };
        }
    </script>
</head>
<body>
<nav>
    <div>
        <a href="/reservations">首页</a>
        <a href="/admin/classroomManagement">教室管理</a>
        <a href="#">用户管理</a>
<%--        用户管理跟教室管理是一样的操作，只不过是表名不一样而已--%>
    </div>
    <div>
        <a href="/user/login">退出登录</a>
    </div>
</nav>
<div>
    <button class="add-classroom-button" onclick="showAddClassroomDialog()">新增教室</button>
</div>
<form class="filter-form" action="${pageContext.request.contextPath}/filterClassrooms" method="get">
    <div>
        <label for="classroomName">教室名称:</label>
        <input type="text" id="classroomName" name="classroomName">
    </div>
    <div>
        <label for="building">教学楼:</label>
        <input type="text" id="building" name="building">
    </div>
    <div>
        <label for="capacity">容量:</label>
        <input type="text" id="capacity" name="capacity">
    </div>
    <div>
        <label for="status">状态:</label>
        <input type="text" id="status" name="status">
    </div>
    <div>
        <input type="submit" value="筛选">
    </div>
</form>
<div class="table-container">
    <table border="1px">
        <tr>
            <th>教室名称</th>
            <th>教学楼</th>
            <th>容量</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        <c:forEach var="classroom" items="${classroomsList}">
            <tr>
                <td>${classroom.classroomName}</td>
                <td>${classroom.building}</td>
                <td>${classroom.capacity}</td>
                <td>${classroom.status}</td>
                <td>
                    <button class="edit-button" >编辑</button>
                    <button class="delete-button" >删除</button>
                </td>
            </tr>
        </c:forEach>
    </table>
</div>
<div class="modal fade" id="editClassroomModal" tabindex="-1" role="dialog" aria-labelledby="editClassroomModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editClassroomModalLabel">编辑教室</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="editClassroomForm">
                    <input type="hidden" id="editId" name="id">
                    <div class="form-group">
                        <label for="editClassroomName">教室名称:</label>
                        <input type="text" class="form-control" id="editClassroomName" name="classroomName" required>
                    </div>
                    <div class="form-group">
                        <label for="editBuilding">教学楼:</label>
                        <input type="text" class="form-control" id="editBuilding" name="building" required>
                    </div>
                    <div class="form-group">
                        <label for="editCapacity">容量:</label>
                        <input type="number" class="form-control" id="editCapacity" name="capacity" required>
                    </div>
                    <div class="form-group">
                        <label for="editStatus">状态:</label>
                        <select class="form-control" id="editStatus" name="status" required>
                            <option value="可用">可用</option>
                            <option value="不可用">不可用</option>
                        </select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="updateClassroom()">保存</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
</body>
</html>